<?php
/**
 *用户调用类
 *@author sulwan@126.com
 *@date 2009-11-20
 */

 class User extends Application{

 public function __construct() {
    parent::__construct();
 }





function apparatusListUser($page,$num,$findkey='',$order='bid',$fields='*') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT ' . $fields . ' FROM ' . '#happy_department';
	$sql .= $where . $order . $limits;
	#
	$rs = $this->_db->getAll($sql);
	return $rs;
}
/**
 *获取用户列表
 */
public function getUser($page,$num,$findkey='',$order='uid',$fields='*') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT ' . $fields . ' FROM ' . '#happy_user';
	$sql .= $where . $order . $limits;
	#
	$rs = $this->_db->getAll($sql);
	return $rs;
}

/**
 *统计用户数量
 */
public function getUserCount($findkey='') {
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	$sql = 'SELECT count(*) FROM #happy_user';
	$sql .= $where;
	return $this->_db->getOne($sql);
}

/**
 *注册用户
 */
public function saveReg($data) {
	$data = array_map('mysql_escape',$data);
	$sql = "INSERT INTO `#happy_user`(`".implode("`,`",array_keys($data))."`)VALUES('".implode("','",array_values($data))."')";
	$id = $this->_db->execute($sql);
	return $id;
}


/**
 * 检测email是否可注册
 *
 * @param string $email
 * @return bool	如果可用，返回true，否则false
 */
public static function checkUserEmail($email){
	$db = new Happy_db();
	if ( empty($email) ){
		return false;
	}
	$sql = "SELECT * FROM `#happy_user` WHERE `email`='".mysql_escape($email)."'";
	$r = $db->getRow($sql);
	if ( empty($r) ){
		return true;
	}
	else {
		return false;
	}
}

/**
 * 检测用户名是否可注册
 *
 * @param string $username
 * @return bool	如果可用，返回true，否则false
 */
public static function checkUserName($username){
	$db = new Happy_db();
	if ( empty($username) ){
		return false;
	}
	$sql = "SELECT * FROM `#happy_user` WHERE `username`='".mysql_escape($username)."'";
	$r = $db->getRow($sql);
	if ( empty($r) ){
		return true;
	}
	else {
		return false;
	}
}


/**
 * 检测用户账号和密码是否可以登陆
 *
 * @param string $email
 * @param string $password
 * @param int $errno
 * @param string $error
 * @return mixed 如果登陆成功，则返回用户的信息数组，否则返回false
 */
public static function checkLogin($email,$password,$fillInfo = false,$field = 'email'){
	$db = new Happy_db();
	$email = trim($email);
	if ( !$email||!$password ){
		return false;
	}
	$sql = "SELECT * FROM `#happy_user`
			WHERE $field ='".mysql_escape($email)."' AND `userPassword`='".mysql_escape(md5($password))."'";
	$user = $db->getRow($sql);
	if(empty($user)) {
		return false;
	}
	if($fillInfo) {
		$user = $this->fillInfo($user);
	}
	return $user;
}

/**
 * 填充用户信息
 *
 * @param array $user_info
 * @return array
 */
public function fillInfo($user_info){
	if ( empty($user_info)||!is_array($user_info) ){
		return false;
	}
	return $user_info;
}

/**
 * 用户登陆，保存登陆信息到cookie
 *
 * @param array $user	用户信息数组，需要包含uid,email,password，也可以是User的对象
 * @param int $life		生存周期
 * @return bool
 */
public static function login($user,$life=0){
	$db = new Happy_db();
	$cookie = Cookie::getInstance();
	if ( !is_array($user)){
		// 如果不是数组
		return false;
	}
	if ( is_array($user) ){
		if ( !isset($user['uid'])||!isset($user['email'])||!isset($user['userPassword'])||$user['uid']<1 ){
			return false;
		}
		$uid = $user['uid'];
		$username = $user['username'];
		$password = $user['password'];
		$email = $user['email'];
	}
	// 设置cookie
	$hash = md5($uid.$GLOBALS['config']['HASHCODE']);
	$cookie->set('uid',$uid,$life);
	$cookie->set('username',$username,$life);
	$cookie->set('email',$email,$life);
	$cookie->set('password',$password,$life);
	$cookie->set('hash',$hash,$life);
	return true;
}

/**
 * 注销登陆，删除登陆cookie信息
 *
 * @return bool
 */
public static function logout(){
	$cookie = Cookie::getInstance();
	$uid = $cookie->get('uid');
	$cookie->delete('uid');
	$cookie->delete('email');
	$cookie->delete('password');
	$cookie->delete('hash');
	return true;
}

/**
 * 获取当前登陆信息
 *
 * @return mixed 如果成功，返回用户对象，否则返回false
 */
public static function checkUserLogin(){
	$db = new Happy_db();
	$cookie = Cookie::getInstance();
	$uid = $cookie->get('uid');
	$email = $cookie->get('email');
	$password = $cookie->get('password');
	$hash = $cookie->get('hash');
	$uid = intval($uid);
	if ( $uid<1 ){
		return false;
	}

	if ( $hash == md5($uid.HASHCODE)){
		$user = new User();
		$user_info = $user->getUserInfo($uid);
		if ( empty($user_info) ){
			return false;
		}
		elseif ( $user_info['email']!=$email ){
			return false;
		}
		elseif ( $user_info['password']!=$password ){
			return false;
		}
		elseif ( $user_info['deleted']=='Y' ){
			return false;
		}
		else {
			return $user;
		}
	}
	return false;
}

/**
 * 获取用户信息
 *
 * @return mixed 如果成功，返回用户对象，否则返回false
 */
public function getUserInfo($uid) {
	$data = getCache(MENU_USERINFO,$uid);
	if(empty($data)) {
		$sql = 'SELECT *  FROM `#happy_user` WHERE `uid` = '.intval($uid);
		$data = $this->_db->getRow($sql);
	}
	setCache(MENU_USERINFO,$uid,$data);
	return $data;
}

/**
 * 删除用户
 *
 * @return mixed 如果成功，返回用户对象，否则返回false
 */
public function delUser($uid) {
	if(empty($uid)) {
		return 0;
	}
	$sql = "DELETE FROM `#happy_user` WHERE `uid` = ".$uid." LIMIT 1";
	$id = $this->_db->execute($sql);
	delCache(MENU_USERINFO,$uid);
	return $id;
}

/**
 *修改用户资料
 */
 public function saveEdit($data,$uid) {
		if(empty($uid)) {
			return 0;
		}
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= '`'.$key.'`'." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_user` SET ".$sqlBody." where   uid =".$uid." LIMIT 1";
		$data = $this->_db->execute($sql);
		delCache(MENU_USERINFO,$uid);
		return $data;
 }


}
 ?>